package applet;
import java.util.ArrayList;

import processing.core.*;

class Ball
{
   float width, height; //region dim
   float x,y,s,dx,dy;
   float rx, ry; //region corner
 
   int id; //id for the ball
   Artist artistName;
   ArrayList<Ball> others;
   int red;
   int green;
   boolean blocked=false;
   public Ball()
   {
	   width = 400;
	   height = 300;
	   x = GLOBAL.processing.random(width/4,3*width/4);
	   y = GLOBAL.processing.random(height/4,3*height/4);
	   s = GLOBAL.processing.random(10,30);
	   dx = GLOBAL.processing.random(-1,1);
	   dy = GLOBAL.processing.random(-1,1);
      //this.x = 30;
     // this.y = 20;
      //this.s=20;
      //this.dx = 3;
      //this.dy = 3;
   }
   public Ball(int crx, int cry, int cwidth,int cheight)
   {
	   width = cwidth;
	   height = cheight;
	   s = GLOBAL.processing.random(20,40);
	   dx = GLOBAL.processing.random(-1,1);
	   dy = GLOBAL.processing.random(-1,1);
	   rx = crx;
	   ry = cry;
	   x = GLOBAL.processing.random(rx+width/4,rx+3*width/4);
	   y = GLOBAL.processing.random(ry+height/4,ry+3*height/4);
      //this.x = 30;
     // this.y = 20;
      //this.s=20;
      //this.dx = 3;
      //this.dy = 3;
   }
   public Ball(int crx, int cry, int cwidth,int cheight, int cid, ArrayList<Ball> cothers, int cartistId)
   {
	   width = cwidth;
	   height = cheight;
	   s = GLOBAL.processing.random(20,40);
	   dx = GLOBAL.processing.random(-1,1);
	   dy = GLOBAL.processing.random(-1,1);
	   rx = crx;
	   ry = cry;
	   x = GLOBAL.processing.random(rx+width/4,rx+3*width/4);
	   y = GLOBAL.processing.random(ry+height/4,ry+3*height/4);
	   id = cid;
	   others = cothers;
	   //
	  
	   artistName =GLOBAL.db.getArtistNameUsingId(cartistId);
	   
      //this.x = 30;
     // this.y = 20;
      //this.s=20;
      //this.dx = 3;
      //this.dy = 3;
   }
   public void move()
   {
	   this.x = this.x+ this.dx;
       this.y = this.y+ this.dy;
       
       if(this.x-this.s/2 < rx || this.x+this.s/2 > rx+width)
       {
    	   if(this.x-this.s/2 < rx)
    		   this.x = rx +this.s/2;
    	   else
    		   this.x = rx + width - this.s/2;
           
          this.dy += (int)(GLOBAL.processing.random(-1,1));
          this.dy = GLOBAL.processing.max(this.dy,-1);
          this.dy = GLOBAL.processing.min(this.dy,1);
          this.dx *= -1;
          draw(255);
          
       }
       if(this.y-this.s/2 < ry || this.y+this.s/2 > ry+height)
       {
    	   if(this.y-this.s/2 <ry)
    		   this.y = ry + this.s/2;
    	   else
    		   this.y = ry +height - this.s/2;
           
          this.dx += (int)(GLOBAL.processing.random(-1,1));
          this.dx = GLOBAL.processing.max(this.dx,-1);
          this.dx = GLOBAL.processing.min(this.dx,1);
          this.dy *= -1;
          draw(255);
       }
       
        
   }
   public void draw()
   {
	   GLOBAL.processing.pushStyle();
	   GLOBAL.processing.fill(0);
	   GLOBAL.processing.stroke(red,255-red,1);
	   GLOBAL.processing.ellipseMode(GLOBAL.processing.CENTER);
	   GLOBAL.processing.ellipse(this.x, this.y, this.s, this.s);
       red=0;
       GLOBAL.processing.popStyle();
       if(!blocked)
       {
    	 collide();
         move();
       }
       if(GLOBAL.mousePressed && PApplet.dist(GLOBAL.processing.mouseX,GLOBAL.processing.mouseY,this.x,this.y)<=this.s)
           {
              this.blocked = !this.blocked;
           }
       
       //if(MouseOver())
       //{
    	   GLOBAL.processing.fill(255);
    	   GLOBAL.processing.textAlign(GLOBAL.processing.CENTER);
    	   GLOBAL.processing.text(artistName.name,x,y+6);
     //  }
    	   if(MouseOver())
    	   {
    		   //GLOBAL.processing.text(artistName.mbid, x, y +5);
    		   GLOBAL.processing.text("played: "+(int)artistName.playCount, x, y-10);
    	   }
   }
   
   public void draw(int col)
   {
	   GLOBAL.processing.pushStyle();
	   GLOBAL.processing.fill(col);
	   GLOBAL.processing.ellipseMode(GLOBAL.processing.CENTER);
	   GLOBAL.processing.ellipse(this.x, this.y, this.s, this.s);
	   GLOBAL.processing.popStyle();
   }
   
	public void collide()
	{
			for(int j=id+1; j<others.size();j++)
			{
				float dx_ = others.get(j).x - x;
				float dy_ = others.get(j).y - y;
				float distance = PApplet.sqrt(dx_*dx_+dy_*dy_);
				float minDist = others.get(j).s + s;
				minDist *= 0.5;
				if(distance<minDist)
				{
					float angle = PApplet.atan2(dy_,dx_);
					float targetX = x + PApplet.cos(angle)*minDist;
					float targetY = y + PApplet.sin(angle)*minDist;
					float ax = (targetX - others.get(j).x)*0.05f;
					float ay = (targetY - others.get(j).y)*0.05f;

					dx-=ax;
					dy-=ay;
					others.get(j).dx += (ax);
					others.get(j).dy += (ay);
				}
				
			}
	}
   public void redraw()
   {
	   x = GLOBAL.processing.random(rx+width/4,rx+3*width/4);
	   y = GLOBAL.processing.random(ry+height/4,ry+3*height/4);
   }
   
   public boolean MouseOver()
   {
	   if(PApplet.dist(GLOBAL.processing.mouseX, GLOBAL.processing.mouseY, x, y)<s/2)
		   return true;
	   else
		   return false;
   }
}